﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CMS.BLL;


public partial class AdminX_Hotel_HotelMan : System.Web.UI.UserControl
{
    private int nDefaultLanguage = Support.GetDefaultLanguage();
    protected void Page_Load(object sender, EventArgs e)
    {
        this.initDataPaging();
        Alert1.HideMessage();
        if (!IsPostBack)
        {
            SetLable();
            BindDataStarRating();
            BindDataToLanguageDropDown();
            ShowMess();
            BindDataToCountryDropdown();
            BindDataToRegionDropdown(0);
            BindDataToCityDropdown(0);
            BindDataToGrid(1);
        }
    }
    private void SetLable()
    {
        btnAddHotel.Text = MultipleLanguage.BTN_ADDNEW[nDefaultLanguage - 1];
        btnHotelPopular.Text = MultipleLanguage.BTN_HOTELPOPULAR[nDefaultLanguage - 1];
        btnUpdate.Text = MultipleLanguage.BTN_Update[nDefaultLanguage - 1];
        lblCountry.Text = MultipleLanguage.LBL_COUNTRY[nDefaultLanguage - 1];
        lblKeyword.Text = MultipleLanguage.LBL_KEYWORD[nDefaultLanguage - 1];
        lblLanguage.Text = MultipleLanguage.LBL_LANGUAGE[nDefaultLanguage - 1];
        lblRegion.Text = MultipleLanguage.LBL_REGION[nDefaultLanguage - 1];
        btnSearch.Text = MultipleLanguage.BTN_SEARCH[nDefaultLanguage - 1];
        lblStarRating.Text = MultipleLanguage.STARRATING[nDefaultLanguage - 1];
        lblCity.Text = MultipleLanguage.LBL_CITY[nDefaultLanguage - 1];
        btnHotelBooking.Text = MultipleLanguage.BTN_HOTELBOOKING[nDefaultLanguage - 1];
    }

    private void BindDataStarRating()
    {
        ddlStarRating.Items.Insert(0, new ListItem(MultipleLanguage.AllTextItem[nDefaultLanguage-1], "0"));
        ddlStarRating.Items.Insert(1, new ListItem("1*", "1"));
        ddlStarRating.Items.Insert(2, new ListItem("2*", "2"));
        ddlStarRating.Items.Insert(3, new ListItem("3*", "3"));
        ddlStarRating.Items.Insert(4, new ListItem("4*", "4"));
        ddlStarRating.Items.Insert(5, new ListItem("5*", "5"));
    }
    private void BindDataToCountryDropdown()
    {
        DataTable dtbCountry = new DataTable();
        Country objCountry = new Country();
        dtbCountry = objCountry.GetALL();

        ddlCountry.DataSource = dtbCountry;
        ddlCountry.DataValueField = "CountryID";
        switch (ConvertData.ConvertToInt(ddlLanguage.SelectedValue))
        {
            case 1:
                ddlCountry.DataTextField = "CountryName1";
                break;
            case 2:
                ddlCountry.DataTextField = "CountryName2";
                break;
            case 3:
                ddlCountry.DataTextField = "CountryName3";
                break;
        }
        ddlCountry.DataBind();
        Utl.SetNonSelectToControl(ddlCountry, MultipleLanguage.AllTextItem[ConvertData.ConvertToInt(ddlLanguage.SelectedValue) - 1], MultipleLanguage.AllTextItem[ConvertData.ConvertToInt(ddlLanguage.SelectedValue) - 1]);
    }
    private void BindDataToRegionDropdown(int nCountry)
    {
        DataTable dtbRegion = new DataTable();
        Region objRegion = new Region();
        dtbRegion = objRegion.SearchByCountry(nCountry);
        ddlRegion.DataSource = dtbRegion;
        ddlRegion.DataValueField = "RegionID";
        switch (ConvertData.ConvertToInt(ddlLanguage.SelectedValue))
        {
            case 1:
                ddlRegion.DataTextField = "RegionName1";
                break;
            case 2:
                ddlRegion.DataTextField = "RegionName2";
                break;
            case 3:
                ddlRegion.DataTextField = "RegionName3";
                break;
        }
        ddlRegion.DataBind();
        if (ConvertData.ConvertToInt(ddlCountry.SelectedValue) <= 0)
            Utl.SetNonSelectToControl(ddlRegion, MultipleLanguage.AllTextItem[ConvertData.ConvertToInt(ddlLanguage.SelectedValue) - 1], MultipleLanguage.AllTextItem[ConvertData.ConvertToInt(ddlLanguage.SelectedValue) - 1]);
        else
        {
            if (dtbRegion.Rows.Count == 0)
                Utl.SetNonSelectToControl(ddlRegion, MultipleLanguage.DDL_SELECTVALUE[ConvertData.ConvertToInt(ddlLanguage.SelectedValue) - 1], MultipleLanguage.DDL_SELECTVALUE[ConvertData.ConvertToInt(ddlLanguage.SelectedValue) - 1]);
        }
    }
    private void BindDataToCityDropdown(int nRegion)
    {
        DataTable dtbCity = new DataTable();
        City objCity = new City();
        dtbCity = objCity.SearchByRegion(nRegion);
        ddlCity.DataSource = dtbCity;
        ddlCity.DataValueField = "CityID";
        switch (ConvertData.ConvertToInt(ddlLanguage.SelectedValue))
        {
            case 1:
                ddlCity.DataTextField = "CityName1";
                break;
            case 2:
                ddlCity.DataTextField = "CityName2";
                break;
            case 3:
                ddlCity.DataTextField = "CityName3";
                break;
        }
        ddlCity.DataBind();
        if ((ConvertData.ConvertToInt(ddlRegion.SelectedValue) <= 0) && (ConvertData.ConvertToInt(ddlCountry.SelectedValue) > 0))
            Utl.SetNonSelectToControl(ddlCity, MultipleLanguage.DDL_SELECTVALUE[ConvertData.ConvertToInt(ddlLanguage.SelectedValue) - 1], MultipleLanguage.DDL_SELECTVALUE[ConvertData.ConvertToInt(ddlLanguage.SelectedValue) - 1]);
        else
            Utl.SetNonSelectToControl(ddlCity, MultipleLanguage.AllTextItem[ConvertData.ConvertToInt(ddlLanguage.SelectedValue) - 1], MultipleLanguage.AllTextItem[ConvertData.ConvertToInt(ddlLanguage.SelectedValue) - 1]);
    }
    private void BindDataToLanguageDropDown()
    {
        Languages objLanguage = new Languages();
        DataTable dtbLanguage = new DataTable();
        dtbLanguage = objLanguage.Search();
        ddlLanguage.DataSource = dtbLanguage;
        ddlLanguage.DataValueField = "LanguageID";
        ddlLanguage.DataTextField = "Description";
        ddlLanguage.DataBind();
    }

    #region Grid processing
    private void BindDataToGrid(int CurrentPageIndex)
    {
        int nItemView = 0;
        int nPageSize = Config.PAGESIZE;
        int nPageCount = Config.PAGECOUNT;
        int nCountry = ConvertData.ConvertToInt(ddlRegion.SelectedValue);
        int nRegion = ConvertData.ConvertToInt(ddlCity.SelectedValue);
        int nStarRating = ConvertData.ConvertToInt(ddlStarRating.SelectedValue);
        string sSearch = ConvertData.ConvertStringToDB(txtKeyword.Text);
        DataTable dtbHotel = new DataTable();
        Hotel objHotel = new Hotel();

        dtbHotel = objHotel.Search(nItemView, sSearch, nCountry, nRegion, 0, nStarRating, CurrentPageIndex, nPageSize, ref nPageCount);
        lblTotalRecord.Text = MultipleLanguage.LBL_TOTAL_ITEM[nDefaultLanguage - 1] + nPageCount + MultipleLanguage.LBL_FOUND[nDefaultLanguage - 1];
        dtgHotel.DataSource = dtbHotel;
        dtgHotel.DataBind();
        Pager1.ItemCount = nPageCount;

        VisiblePaging(nPageCount);
        if (dtbHotel.Rows.Count > 0)
            tblWarning.Visible = false;
        else
            tblWarning.Visible = true;
    }
    protected void dtgHotel_ItemCommand(object source, DataGridCommandEventArgs e)
    {
        int nDefaultLanguage = Support.GetDefaultLanguage() - 1;
        string strCommand = e.CommandName;
        int nID = ConvertData.ConvertToInt(e.CommandArgument);
        Hotel obj = new Hotel();
        obj.LoadById(nID);
        switch (strCommand)
        {
            case "Delete":
                if (obj.Data.Image != "")
                    Support.DeleteFile("hotel", obj.Data.Image);
                HotelImages objHotelImg = new HotelImages();
                int nPageCount = 0;
                DataTable dtbHotelImg = objHotelImg.Search(nID, 1, 1000, ref nPageCount);
                foreach (DataRow dtr in dtbHotelImg.Rows)
                {
                    string str = dtr["FileName"].ToString();
                    if (dtr["FileName"].ToString() != "")
                        Support.DeleteFile("hotel", dtr["FileName"].ToString());
                }

                int nDelete = obj.DeleteById(nID);
                if (nDelete > 0)
                    Alert1.ShowMessage(1, MessagesAlert.DELETE_SUCCESSFULL[nDefaultLanguage]);
                else
                    Alert1.ShowMessage(2, MessagesAlert.DELETE_UNSUCCESSFULL[nDefaultLanguage]);
                BindDataToGrid(Pager1.CurrentIndex);
                break;

            case "Edit":
                string sEdit = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_HOTEL_ADD + "&" + Config.ACTION + "=" + Config.ACTION_EDIT + "&" + Config.ACTION_ID + "=" + nID;
                Response.Redirect(sEdit);
                break;
            case "Active":
                int nActive = obj.UpdateStatus(nID, EnumeType.INACTIVE);
                if (nActive > 0)
                    Alert1.ShowMessage(1, MessagesAlert.INACTIVE_SUCCESSFUL[nDefaultLanguage]);
                else
                    Alert1.ShowMessage(2, MessagesAlert.INACTIVE_UNSUCCESSFUL[nDefaultLanguage]);
                BindDataToGrid(Pager1.CurrentIndex);
                break;

            case "Inactive":
                int nInactive = obj.UpdateStatus(nID, EnumeType.ACTIVE);
                if (nInactive > 0)
                    Alert1.ShowMessage(1, MessagesAlert.ACTIVE_SUCCESSFUL[nDefaultLanguage]);
                else
                    Alert1.ShowMessage(1, MessagesAlert.ACTIVE_UNSUCCESSFUL[nDefaultLanguage]);
                BindDataToGrid(Pager1.CurrentIndex);
                break;

        }
    }
    protected void dtgHotel_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        int nDefaultLanguage = Support.GetDefaultLanguage() - 1;
        int nLangID = ConvertData.ConvertToInt(ddlLanguage.SelectedValue);
        if ((e.Item.ItemType != ListItemType.Header) && (e.Item.ItemType != ListItemType.Footer))
        {
            Label lblTitle = (Label)e.Item.FindControl("lblTitle");
            Label lblStarRating = (Label)e.Item.FindControl("lblStarRating");
            TextBox txtPriority = (TextBox)e.Item.FindControl("txtPriority");
            LinkButton lnkEdit = (LinkButton)e.Item.FindControl("lnkEdit");
            LinkButton lnkDelete = (LinkButton)e.Item.FindControl("lnkDelete");
            LinkButton lnkActive = (LinkButton)e.Item.FindControl("lnkActive");
            LinkButton lnkInactive = (LinkButton)e.Item.FindControl("lnkInactive");
            Label lblCountry = (Label)e.Item.FindControl("lblCountry");
            Label lblRegion = (Label)e.Item.FindControl("lblRegion");
            Label lblCity = (Label)e.Item.FindControl("lblCity");
            Country objCountry = new Country();
            Region objReion = new Region();
            City objCity = new City();
            objCity.LoadById(ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "CityID")));
            objReion.LoadById(objCity.Data.RegionID);
            objCountry.LoadById(objReion.Data.CountryID);
            
            Label lblID = (Label)e.Item.FindControl("lblID");
            int nID = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "HotelID"));
            int nStatus = ConvertData.ConvertToInt(DataBinder.Eval(e.Item.DataItem, "Status"));
            string nPriority = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Priority"));

            lblStarRating.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "StarRating"));
            txtPriority.Text = nPriority;
            switch (nLangID)
            {
                case 1:
                    lblCountry.Text = objCountry.Data.CountryName1;
                    lblRegion.Text = objReion.Data.RegionName1;
                    lblCity.Text = objCity.Data.CityName1;
                    lblTitle.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Title1"));
                    break;
                case 2:
                    lblCountry.Text = objCountry.Data.CountryName2;
                    lblRegion.Text = objReion.Data.RegionName2;
                    lblCity.Text = objCity.Data.CityName2;
                    lblTitle.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Title2"));
                    break;
                case 3:
                    lblCountry.Text = objCountry.Data.CountryName3;
                    lblRegion.Text = objReion.Data.RegionName3;
                    lblCity.Text = objCity.Data.CityName3;
                    lblTitle.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "Title3"));
                    break;
            }
            lblID.Text = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "HotelID"));
            if (nStatus > 0)
            {
                lnkActive.Visible = true;
                lnkInactive.Visible = false;
            }
            else
            {
                lnkActive.Visible = false;
                lnkInactive.Visible = true;
            }

            lnkActive.Text = MultipleLanguage.STATUS_ACTIVE[nDefaultLanguage - 1];
            lnkActive.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "HotelID"));
            lnkActive.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.INACTIVE_ALERT[nDefaultLanguage - 1]);
            lnkInactive.Text = MultipleLanguage.STATUS_INACTIVE[nDefaultLanguage - 1];
            lnkInactive.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "HotelID"));
            lnkInactive.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.ACTIVE_ALERT[nDefaultLanguage - 1]);

            lnkDelete.Text = MultipleLanguage.BTN_DELETE[nDefaultLanguage - 1];
            lnkDelete.Enabled = true;
            lnkDelete.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "HotelID"));
            lnkDelete.OnClientClick = Support.CreateConfirmBoxClient(MessagesAlert.DELETE_CATEGORY_CONFIRM[nDefaultLanguage]);
            
            lnkEdit.Text = MultipleLanguage.BTN_EDIT[nDefaultLanguage - 1];
            lnkEdit.CommandArgument = ConvertData.ConvertToString(DataBinder.Eval(e.Item.DataItem, "HotelID"));
        }
    }
    #endregion

    #region Click Events

    protected void btnAddHotel_Click1(object sender, EventArgs e)
    {
        string sAdd = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_HOTEL_ADD + "&" + Config.ACTION + "=" + Config.ACTION_ADD;
        Response.Redirect(sAdd);
    }
    protected void btnAddHotelPopular_Click1(object sender, EventArgs e)
    {
        string sAdd = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_HOTELPOPULAR + "&" + Config.ACTION + "=" + Config.ACTION_ADD;
        Response.Redirect(sAdd);
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();
        // Update  data
        int nCount = dtgHotel.Items.Count;

        for (int i = 0; i < nCount; i++)
        {
            TextBox txtPriority = (TextBox)dtgHotel.Items[i].FindControl("txtPriority");
            Label lblID = (Label)dtgHotel.Items[i].FindControl("lblID");

            int nTourID = ConvertData.ConvertToInt(lblID.Text);
            int nPriority = ConvertData.ConvertToInt(txtPriority.Text);

            Hotel objTour = new Hotel();
            int nResult = objTour.UpdatePriority(nTourID, nPriority);

            if (nResult > 0) { Alert1.ShowMessage(1, MessagesAlert.UPDATE_SUCCESSFUL[nDefaultLanguage - 1]); }
            else { Alert1.ShowMessage(2, MessagesAlert.UPDATE_UNSUCCESSFUL[nDefaultLanguage - 1]); }
        }
        this.BindDataToGrid(1);
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindDataToGrid(1);
    }
    protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDataToCountryDropdown();
        BindDataToRegionDropdown(0);
        BindDataToGrid(1);
    }
    protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {
        int nCountry = ConvertData.ConvertToInt(ddlCountry.SelectedValue);
        BindDataToRegionDropdown(nCountry);
        int nRegion = ConvertData.ConvertToInt(ddlRegion.SelectedValue);
        BindDataToCityDropdown(nRegion);
    }
    protected void ddlRegion_SelectedIndexChanged(object sender, EventArgs e)
    {
        int nRegion = ConvertData.ConvertToInt(ddlRegion.SelectedValue);
        BindDataToCityDropdown(nRegion);
    }
    #endregion
    
    #region Paging & Message
    private void initDataPaging()
    {
        Pager1.PageSize = Config.PAGESIZE;
        Pager1.ShowFirstLast = true;
        Pager1.FirstClause = "Đầu";
        Pager1.LastClause = "Cuối";
        Pager1.NextClause = "Tiếp";
        Pager1.OfClause = "của";
        Pager1.PageClause = "Trang";
        Pager1.CompactedPageCount = 5;
        Pager1.PreviousClause = "Sau";
        Pager1.MaxSmartShortCutCount = 5;
        Pager1.NotCompactedPageCount = 5;
    }

    private void VisiblePaging(int intPagecount)
    {
        int nPageSize = 0;
        nPageSize = Config.PAGESIZE;
        if (intPagecount > nPageSize)
            Pager1.Visible = true;
        else
            Pager1.Visible = false;
    }

    public void pager_Command(object sender, CommandEventArgs e)
    {
        int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
        Pager1.CurrentIndex = currnetPageIndx;
        BindDataToGrid(currnetPageIndx);
    }

    private void ShowMess()
    {
        int nDefaultLanguage = Support.GetDefaultLanguage();
        int nMessage = ConvertData.ConvertToInt(Request.QueryString[Config.MESS_ID]);
        lblWarning.Text = MessagesAlert.SEARCH_NO_ITEM[nDefaultLanguage];
        switch (nMessage)
        {
            case 1:
                Alert1.ShowMessage(1, MessagesAlert.INSERT_SUCCESSFULL[nDefaultLanguage]);
                break;
            case 2:
                Alert1.ShowMessage(1, MessagesAlert.UPDATE_SUCCESSFUL[nDefaultLanguage]);
                break;
            case 3:
                Alert1.ShowMessage(2, MessagesAlert.ITEM_NOTEXIST[nDefaultLanguage]);
                break;
            default:
                Alert1.HideMessage();
                break;
        }
    }
    #endregion
    protected void btnHotelBooking_Click1(object sender, EventArgs e)
    {
        string sLink = Config.ROOT + Pages.BackEnds.ADMIN + "?" + Config.PAGE + "=" + Pages.BackEnds.STR_BOOKINGHOTEL;
        Response.Redirect(sLink);
    }
}